package com.student;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Administrator
 */
public class SetExtension implements Function {
    static ArrayList<User> arrayList = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    FileWriter fw = new FileWriter("D://student.txt", true);

    public SetExtension() throws IOException {
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("**********");
            System.out.println("19APP4 Mucd ");
            System.out.println("管  理  系  统");
            System.out.println("**********");
            System.out.println("1.添加信息 2.删除信息 3.修改信息 4.查询所有信息 5.查詢指定ID信息 0.退出");
            Scanner sc = new Scanner(System.in);
            int scInt = sc.nextInt();
            switch (scInt) {
                case 1:
                    insert();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    select();
                    break;
                case 0:
                    System.out.println("正在退出 ... ");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入数字使用功能");
            }
        }
    }


    /**
     * 创建学生对象
     */

    @Override
    public void select() {

    }

    @Override
    public void delete() {

    }


    /**
     * 判断用户名是否重复
     */
    public boolean isUser(String id) {
        boolean flag = false;
        for (User user1 : arrayList) {
            if (user1.getUser().equals(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    int id;

    @Override
    public void insert() {
        Scanner sc = new Scanner(System.in);
        User user = new User();

        System.out.println("输入用户名");
        String scUser = sc.next();
        System.out.println("输入密码");
        String scPasswd = sc.next();
        while (true) {
            boolean flag = isUser(String.valueOf(id));
            if (flag) {
                System.out.println("ID重复");
            } else {
                //ID不重复
                user.setId(String.valueOf(id += 1));
                user.setUser(scUser);
                user.setPasswd(scPasswd);
                arrayList.add(user);

                for (User u : arrayList) {
                    System.out.println("ID-" + u.getId() + ", " + "用户名-" + u.getUser() + ", " + "密码-" + u.getPasswd());
                    try {
                        fw.write("ID-" + u.getId() + ", " + "用户名-" + u.getUser() + ", " + "密码-" + u.getPasswd() + "\r\n");
                        fw.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
            }
        }

    }

    @Override
    public void update() {

    }
}
